﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marmat.Forms
{
    public partial class SearchControl
    {

        public class Field : ISearchControlField
        {
            /// <summary>
            /// 列名
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// 列标题
            /// </summary>
            public string Caption { get; set; }

            /// <summary>
            /// 数据类型
            /// </summary>
            public Type DataType { get; set; }

            /// <summary>
            /// 获取或设置分组的搜索条件。
            /// </summary>
            public string Condition { get; set; }

            /// <summary>
            /// 转换
            /// </summary>
            /// <param name="txt"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            public bool ConvertValue(string txt, out object value)
            {
                value = null;
                Type type = this.DataType;
                if (this.DataType == typeof(string))
                    value = txt;
                else if ((type == typeof(int) || type == typeof(decimal) || type == typeof(float) || type == typeof(double) || type == typeof(Int64) || type == typeof(Single)) && int.TryParse(txt, out int val_int))
                {
                    value = val_int;
                }
                else if (type == typeof(DateTime) && DateTime.TryParse(txt, out DateTime val_time))
                {
                    value = val_time;
                }
                else if (type == typeof(bool) && (bool.TryParse(txt, out bool val_bool) || txt == "是" || txt == "否" || txt == "1" || txt == "0"))
                {
                    if (txt == "是" || txt == "1") value = true;
                    else if (txt == "否" || txt == "0") value = false;
                    else value = val_bool;
                }
                return value != null;
            }
        }
    }
}
